//cadence格式输出
var downDra=(function(){
    var sl_a="&&",
        sl_b="&",
        sl_c="~",
        sl_d="$",
        sl_e="+",
        sl_f="-",
        sl_g=":",
        sl_i="(",
        sl_j=")",
        sl_k=" ",
        sl_m="\r\n",
        sl_n="\n",
        sl_o="\"";
    var mil_mm = "";
	var tidy={
        //检测对象是否为空
        isEmpty:function(obj){
            let lock=false;
            if(!obj){
                return true;
            }
            if(JSON.stringify(obj) === "{}"){
                lock=true;
            }
            if(Object.getOwnPropertyNames(obj).length==0){
                lock=true;
            }
            return lock;
        },
        //pad排序
		setOrder:function(pads,pins){
			let newPads=[],
				newPins=[];
			pins=pins.sort(function(a, b){return a.pin_padid - b.pin_padid});//.sort(function(a, b){return a.id - b.id});
            pads=pads.sort(function(a, b){return a.id - b.id});
            let lock_edit=0,
                l_num=1;
			
            pads.forEach(function(i_d,i){
                let ceshi_l=false;
                pins.forEach(function(k_d,k){
                    if(k_d.pin_padid==i_d.id){
                        ceshi_l=true;
                        let pi_n=k_d;
                        pi_n.pin_padid=l_num;
                        newPins.push(pi_n);
                    }
                });
                if(ceshi_l){//pad必须引用才保存
                    let pa_d=i_d;
                    pa_d.id=l_num;
                    newPads.push(pa_d);
                    l_num+=1;
                }
            });
			return {
				pads:newPads,
				pins:newPins,
			};
		},
        //去除圆弧中心点到线段起点的短线
		getPathsNew:function(path){
            var paths=[],
                arcs=[];

            path.forEach(function(a_d,a){
                if(a_d.name?.toUpperCase()=="ARC"){
                    arcs.push(a_d);
                }
            });
            path.forEach(function(a_d,a){
                let lock=true;
                arcs.forEach(function(b_d,b){
                    if(a_d.sp_x==b_d.o_x&&a_d.sp_y==b_d.o_y){
                        lock=false;
                    }
                });
                if(lock){
                   paths.push(a_d);
                }
            });
            
            return paths;
        },
        //纠正起始为
        sortLine:function(paths){
            
        },
		//处理pins的输出格式
        setpins:function(pins){
            let pinid="",
                pinBuff="",
                pinsbuff ="";
            pins.forEach((a_d,a)=>{//pins整合，单独属性以~隔开
                pinid=a_d.pin_id;
                if(pinid==""||pinid==null||!pinid||pinid.length==0){
                    pinid="___";
                }
                pinBuff=pinid+sl_c+a_d.pin_padid+sl_c+a_d.rotate+sl_c+a_d.x+sl_c+a_d.y;
                if(a<pins.length-1){
                    pinBuff=pinBuff.concat(sl_e);//每个pin数据以+隔开
                }
                pinsbuff=pinsbuff.concat(pinBuff);
            });
            return pinsbuff;
        },
        //处理pads的输出格式
        setPads:function(pads){
            var padsbuff="",
                pad_buff="",
                drill_buff="",
                drill=[],
                padLayer_buff="";
               
            pads.forEach(function(pad,b){//pads整合
                drill=pad.drill;
                if(drill.length==0){//每个pin的过孔
                    drill_buff="nil";
                }else{
                    drill.forEach(function(hole,c){//钻孔信息整合，单独属性以~隔开
                        drill_buff=tidy.setPadDirll(hole,pad.name)
                    });
                }
                padLayer_buff = tidy.setPadLayer(pad);

                //id+钻孔+外径
                pad_buff=pad.id+sl_e+drill_buff+sl_e+padLayer_buff;//一整个pad的描述
                if(b<pads.length-1){
                    pad_buff=pad_buff.concat(sl_a);//每个pad数据以&&隔开
                }
                padsbuff=padsbuff.concat(pad_buff);//封装的所有pad数据
            });
            return padsbuff;
        },
        //处理过孔
        setPadDirll:function(drill,padName){
            var h_w=0,h_h=0;
            var multidata=drill.multidata;
            if(!multidata){
                multidata="nil";
            }
            var figure=drill.figure.name;
            if(figure == "NULL"){
                figure = "nil";
            }
            h_w=drill.figure.w;
            h_h=drill.figure.h;
            let drill_buff=drill.uvia+sl_c
                +drill.keepout+sl_c
                +drill.diameter+sl_c
                +figure+sl_c
                +h_w+sl_c
                +h_h+sl_c
                +drill.offset.x+sl_c
                +drill.offset.y+sl_c
                +drill.plate+sl_c
                +multidata+sl_c
                +drill.hole.form+sl_c
                +drill.hole.w+sl_c
                +drill.hole.h+sl_c
                +drill.tolerance.x+sl_c
                +drill.tolerance.y+sl_c
                +padName;
            return drill_buff;
        },
        //处理pad某层数据
        setPadLayer:function(pad){
            //需要抽取的层
            let layerlist=[["t",["t","ts_t","tp_t"]],["b",["b","bs_b","bp_b"]]]
            let p_layer="nil";
            var t_buff="nil",
                b_buff="nil",
                tst_buff="nil",
                tpt_buff="nil",
                bst_buff="nil",
                bpt_buff="nil"; 
            layerlist.forEach((layer,a)=>{
                let layerData=pad[layer[0]];
                let inLayerList = layer[1];
                //遍历内层
                inLayerList.forEach((inlayer,b)=>{

                    let inlayerData = layerData[inlayer];
                    if(!tidy.isEmpty(inlayerData)){
                        let p_form=inlayerData.form?.toLowerCase();
                        if(p_form!="path"){
                            p_layer = tidy.setForm(p_form,inlayerData);
                        }else if(p_form=="path"){
                            p_layer = tidy.setPath(inlayerData);
                        }
                    }else{
                        p_layer = "nil";
                    }

                    switch(inlayer){
                        case "t":
                            t_buff=p_layer;
                            break;
                        case "ts_t":
                            tst_buff=p_layer;
                            break;
                        case "tp_t":
                            tpt_buff=p_layer;
                            break;
                        case "b":
                            b_buff=p_layer;
                            break;
                        case "bs_b":
                            bst_buff=p_layer;
                            break;
                        case "bp_b":
                            bpt_buff=p_layer; 
                            break;
                    }
                })
            });

            return t_buff+sl_e+b_buff+sl_e+tst_buff+sl_e
                   +bst_buff+sl_e+tpt_buff+sl_e+bpt_buff;;  
        },
        //处理标准焊盘的数据
        setForm:function(form,obj){
            if(tidy.isEmpty(obj)){
                return "nil";
            }
            let p_w=obj.w;
            let p_h=obj.h;
            let p_rot=obj.rotate;
            let p_swx=obj.skewing_x;
            let p_swy=obj.skewing_y;
            //(pads.t.form (pads.t.w pads.t.h) (0.0 0.0) 0 0) 
            return sl_i+form.toUpperCase()+sl_k+sl_i+p_w+sl_k+p_h+sl_j+sl_k+sl_i+p_swx+sl_k+p_swy+sl_j+sl_k+p_rot+sl_k+0+sl_j;
        },
        //处理自定义焊盘的数据
        setPath:function(obj){
            let p_path = obj.path;
            let str_1,
                str_2,
                str_3,
                str_4,
                str_5,
                str_6,
                str_7,
                str_8,
                str_9,
                str_0;
            if(!p_path||p_path.length==0){
                return "nil";
            }else{
                let path_layer="",
                    p_layer="";
                    p_path=tidy.getPathsNew(p_path);
                p_path.forEach((pathb_d,path_b)=>{
                    str_1=pathb_d.width;
                    str_2=pathb_d.sp_x;
                    str_3=pathb_d.sp_y;
                    str_4=pathb_d.r;
                    str_5=pathb_d.arc_r;
                    str_6=pathb_d.o_x;
                    str_7=pathb_d.o_y;
                    str_8=0;//针对圆弧
                    str_0=pathb_d.name;
                    if(mil_mm=="mm"){
                        str_4=str_4/1;
                    }
                    
                    str_0=str_0.toUpperCase();
                    if(path_b==0){
                        if(str_0=="LINE"){
                            path_layer="path"+sl_k+sl_i+str_2+sl_k+str_3+sl_k+str_4+sl_k+str_1+sl_k+str_5+sl_j;
                        }else if(str_0=="ARC"){
                            path_layer="path"+sl_k+sl_i+str_6+sl_k+str_7+sl_k+str_4+sl_k+str_1+sl_k+str_5+sl_j+sl_k
                            +sl_i+str_2+sl_k+str_3+sl_k+str_8+sl_k+str_1+sl_k+str_5+sl_j;
                        }
                        
                    }else{
                        if(str_0=="LINE"){
                            path_layer=sl_i+str_2+sl_k+str_3+sl_k+str_4+sl_k+str_1+sl_k+str_5+sl_j; 
                        }else if(str_0=="ARC"){
                            path_layer=sl_i+str_6+sl_k+str_7+sl_k+str_4+sl_k+str_1+sl_k+str_5+sl_j+sl_k
                            +sl_i+str_2+sl_k+str_3+sl_k+str_8+sl_k+str_1+sl_k+str_5+sl_j;
                        }   
                    } 
                    if(path_b<p_path.length-1){
                        path_layer=path_layer.concat(sl_k);//路径以空格隔开
                    }
                    p_layer=p_layer.concat(path_layer);
                });
                return sl_i+p_layer+sl_j;
            }
        },
        //处理外部添加层的数据
        setMainLayer:function(img){
            let layerList = [["asm",["at","ab"]],["ss",["st","sb"]],
                            ["b",["bt","bb"]],["e",["et","eb"]],
                            ["p",["pt","pb"]],["sm",["sm_t","sm_b"]]
                        ];
            let layerbuff="";
            layerList.forEach((inlayer)=>{
                let LayerName = inlayer[0];
                let inLayerList = inlayer[1];
                inLayerList.forEach((layer)=>{
                    let layerData = img[LayerName][layer];
                    let layerTxt = tidy.setLayer(layerData);
                    if(layer=="sm_b"){
                        layerbuff = layerbuff + layerTxt;
                    }else{
                        layerbuff = layerbuff + layerTxt+ sl_m
                    }
                });
                
            });

            return layerbuff;
        },
        //梳理外部层一层的数据
        setLayer:function(layerData){
            let layerBuff="";
            let typeList = ["path","shapes","texts"]

            typeList.forEach((type)=>{
                let typeBuff = "";
                let typeArr = layerData[type];
                if(typeArr.length&&typeArr.length>0){
                    switch (type) {
                        case "path":
                            typeBuff = tidy.setLayerLine(typeArr);
                            break;
                        case "shapes":
                            typeBuff = tidy.setLayerShape(typeArr);
                            break;
                        case "texts":
                            typeBuff = tidy.setLayerText(typeArr);
                            break;
                    
                        default:

                            break;
                    }

                }else{
                    typeBuff = "nil";
                }
                if(type=="texts"){
                    layerBuff = layerBuff + typeBuff;
                }else{
                    layerBuff = layerBuff + typeBuff +sl_k;
                }
            });
            // + path_buff + sl_k + shape_buff + sl_k + text_buff + sl_j;
            return sl_i+layerBuff+sl_j;//(layerBuff)
        },
        //处理外部层的线 
        setLayerLine(lines){
            let path_buff = "";
            lines.forEach((line,index)=>{
                let str_0 = line.name.toLowerCase();
                let str_1 = mil_mm=="mm"?0.127:5;//line.width;
                let str_2 = line.st_x;
                let str_3 = line.st_y;
                let str_4 = line.sp_x;
                let str_5 = line.sp_y;
                let str_6 = line.o_x;
                let str_7 = line.o_y;
                let str_8 = line.r;
                let str_9 = line.arc_r;
                let path = sl_i
                    + str_0 + sl_k
                    + str_1 + sl_k
                    + str_2 + sl_k
                    + str_3 + sl_k
                    + str_4 + sl_k
                    + str_5 + sl_k
                    + str_6 + sl_k
                    + str_7 + sl_k
                    + str_8 + sl_k
                    + str_9 + sl_k
                    + sl_j;

                path_buff = path_buff + path;
                if(index < line.length-1){
                    path_buff = path_buff + sl_k;	
                }
            });
            return sl_i + path_buff + sl_j;
        },
        //处理外部层的字
        setLayerText(texts){
            let text_buff ="";
            texts.forEach((txt,index)=>{
                let str_1 = txt.name;
                let str_2 = txt.content.trim();
                let str_3 = txt.textblock;
                let str_4 = txt.textheight;
                let str_5 = txt.textwidth;
                let str_6 = txt.x;
                let str_7 = txt.y;
                let str_0 = txt.rotate;
                let text = sl_i
                    + str_1 + sl_k
                    +sl_o+ str_2 +sl_o+ sl_k    //"txt"
                    + str_3 + sl_k
                    + str_4 + sl_k
                    + str_5 + sl_k
                    + str_6 + sl_k
                    + str_7 + sl_k
                    + str_0 + sl_k
                    + sl_j;
                text_buff = text_buff + text;
                if(index<texts.length-1){
                    text_buff = text_buff + sl_k;
                }
            }); 
            return sl_i + text_buff +sl_j;
        },
        //处理外部层的铜皮
        setLayerShape(shapes){
            let typeList=["outline","fill"];
            let shape_buff ="";
            shapes.forEach((shape,index)=>{//每一块铜皮
                let outl_buff = "nil";
                let fill_buff = "nil";
                typeList.forEach((type)=>{//填充部分以及挖空区域
                    let typeArr = shape[type];
                    if(typeArr.length&&typeArr.length>0){
                        let typeBuff="";

                        if(typeArr[0] instanceof Array){//兼容多个挖空区域的数据
                            typeBuff = typeBuff+"arr";
                            let oneBuff = "";
                            typeArr.forEach((fill,fillnum)=>{
                                oneBuff = oneBuff + tidy.setShape(fill);

                            });
                            typeBuff = sl_i+typeBuff + oneBuff+sl_j;
                        }else{
                            typeBuff = tidy.setShape(typeArr);
                        }
                        
                        if(type=="fill"){
                            fill_buff = typeBuff;
                        }else if(type=="outline"){
                            outl_buff = typeBuff;
                        }
                    }
                    
                });
                let  oneshape = sl_i+outl_buff+sl_k+fill_buff+sl_j;
                shape_buff = shape_buff + oneshape;
                if(index<shapes.length-1){
                    shape_buff = shape_buff + sl_k;
                }
            });
            return sl_i+shape_buff+sl_j;
        },
        //梳理一块铜皮的区域
        setShape(shapeArr){
            let buff =""
            shapeArr.forEach((line,index)=>{// 具体线
                let str_0 = line.name.toLowerCase();
                let str_1 = line.width;
                let str_2 = line.st_x;
                let str_3 = line.st_y;
                let str_4 = line.sp_x;
                let str_5 = line.sp_y;
                let str_6 = line.o_x;
                let str_7 = line.o_y;
                let str_8 = line.r;
                let str_9 = line.arc_r;
                let s_line = sl_i
                    + str_0 + sl_k
                    + str_1 + sl_k
                    + str_2 + sl_k
                    + str_3 + sl_k
                    + str_4 + sl_k
                    + str_5 + sl_k
                    + str_6 + sl_k
                    + str_7 + sl_k
                    + str_8 + sl_k
                    + str_9 + sl_k
                    + sl_j;
                buff = buff + s_line;
                if(index<shapeArr.length-1){
                    buff = buff + sl_k;
                }
            });
            return sl_i + buff + sl_j;
        },
        //追加sm层数据
        checkSmLayer(img){
            if(!img.sm){
                img.sm={
                    sm_t:{
                        texts:[],
                        shapes:[],
                        path:[],
                    },
                    sm_b:{
                        texts:[],
                        shapes:[],
                        path:[],
                    },
                }
            }
            return img;
        }
	}
    function getDownData(data,id,filename){//完整的封装数据，数据id
        var name="",
            jpid="",
            z_height="nil",
            string_sum="";
        var img={};
        mil_mm=data.units;
        //取出图形数据
        if(data.img_data instanceof Array){
            img=data.img_data[0];
        }else{
            img=data.img_data;
        }

        img=tidy.checkSmLayer(img)
        //如果有高度信息
        if(img.header.z_height){
            z_height=img.header.z_height;
        }
        if(!filename){
            if(data.componet_id){
                jpid="jpid:"+id;
                name=data.write_id;
            }else{
                jpid="jpid:"+id;
                name=data.en_uid+id;
            }
        }else{
            name = filename;
        }
        //替换特殊字符
        name=name.replace(/\(|\!|\.|\%|\@|\^|\)|\）|\ |\/|\\|\（|\=|\+|\<|\>|\,|\?|\*|\$|\}|\{|\]|\[|\#|\、|\&/g,"_");
        //整理数据
        let newPad = tidy.setOrder(img.pads,img.pins);
        var pin_buff = tidy.setpins(newPad.pins);
        var pad_buff = tidy.setPads(newPad.pads);
        var layer_buff = tidy.setMainLayer(img);
        if(z_height==""||z_height==null){
            z_height="nil";	
        }
        //拼接数据返回
        string_sum=name + sl_m + 
            jpid + sl_m + 
            mil_mm + sl_m +
            z_height + sl_m +
            pin_buff + sl_m + pad_buff + sl_m + layer_buff;
        return string_sum;
    }

	return {
		getDra:getDownData,
	};
})();
